<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>JVM学习05-垃圾回收器和内存分配 - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="JVM学习05-垃圾回收器和内存分配">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="JVM学习05-垃圾回收器和内存分配 - 天地维杰网" />
    <meta name="description" content=" 一、串行回收器 串行回收器成熟、历经生产考验、极为高效，jdk中最基本的垃圾回收器之一，特点：仅使用单线程进行垃圾回收，独占式回收：stop the world，回收时java应用程序中的线程都需要暂停等垃圾回收完成。" />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/jvm-study-05-garbage-collector-and-memory-allocation/" itemprop="url">
        JVM学习05-垃圾回收器和内存分配
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2022-10-12">
    2022-10-12
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/java" itemprop="url" rel="index">
        <span itemprop="name">java</span>
      </a>
      &nbsp; 
    </span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/jvm" itemprop="url" rel="index">
        <span itemprop="name">jvm</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
 <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">原创</span>
</span>

       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">9471 字 ~19分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h2 id="一-串行回收器">一、串行回收器</h2>

<p>串行回收器成熟、历经生产考验、极为高效</p>

<p>jdk中最基本的垃圾回收器之一</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625646471224-20210707162750.png" width = 400 /> </div>

<p>特点：</p>

<ul>
<li><p>仅使用单线程进行垃圾回收</p></li>

<li><p>独占式回收：stop the world，回收时java应用程序中的线程都需要暂停等垃圾回收完成。</p></li>
</ul>

<ol>
<li><strong>新生代串行回收</strong></li>
</ol>

<p>新生代串行回收器使用复制算法，实现相对简单、逻辑处理高效且没有线程切换的开销。</p>

<p>使用<code>-XX:+UseSerialGC</code>参数可以指定使用新生代串行收集器和老年代串行收集器。当虚拟机在Client模式下运行时，它是默认的垃圾收集器。</p>

<p>输出GC日志格式为</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   [GC 0.844:[DefNew:]]</code></pre></div>
<ol>
<li><strong>老年代串行回收器</strong></li>
</ol>

<p>老年代串行回收器使用标记压缩算法，也是串行的、独占式的垃圾回收器，回收比新生代使用更长时间。</p>

<p>可以与多种新生代回收器配合使用：</p>

<ul>
<li><code>-XX:+UseSerialGC</code>:新、老都使用串行回收</li>
<li><code>-XX:+UseParNewGC</code>:新生代使用<code>ParNew</code>回收器，老年代使用串行</li>

<li><p><code>-XX:+UseParalleelGC</code>:新生代使用<code>ParallelGC</code>回收器，老年代使用串行</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">[Full GC 0.844:[Tenured: xxxk-&gt;xxxk(xxxk)]]</code></pre></div></li>
</ul>

<h2 id="二-并行回收器">二、并行回收器</h2>

<p>使用多个线程进行垃圾回收，对于并行 能力强的计算机，可以有效缩减回收时间。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625646363526-20210707162603.png" width = 400 /> </div>

<ol>
<li><strong>新生代ParNew回收器</strong></li>
</ol>

<p>将串行回收器多线程化，回收策略、算法、参数与新生代串行回收器一样（复制算法）。也独占，也STW，CPU并发强停顿时间就短，单CPU或CPU并发能力弱表现可能比串行回收还差。</p>

<p>开启使用以下参数：</p>

<ul>
<li><code>-XX:+UseParNewGC</code>: 新生代使用ParNew回收器，老年代使用串行回收</li>
<li><code>-XX:+UseConcMarkSweepGC</code>: 新生代使ParNew回收器，老年代使用CMS</li>
</ul>

<p>回收线程数量可以使用<code>-XX:ParallelGCThreads</code> 参数指定。如<code>-XX:ParallelGCThreads=8</code></p>

<ul>
<li><p>一般最好与CPU数量相当，避免过多的线程数影响回收性能。</p></li>

<li><p>默认情况下当CPU数量小于8个时，<code>ParallelGCThreads</code>的值等于CPU数量。</p></li>

<li><p>当CPU数大于8个时，ParallelGCThreads的值等于 3+((5*CPU_COUNT)/8)</p></li>
</ul>

<p>日志格式：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   <span style="color:#ce5c00;font-weight:bold">[</span>GC 0.834: <span style="color:#ce5c00;font-weight:bold">[</span>ParNew: xxxK -&gt; xxxK<span style="color:#ce5c00;font-weight:bold">(</span>xxxK<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">]</span></code></pre></div>
<ol>
<li><strong>新生代ParallelGC回收器</strong></li>
</ol>

<p>新生代<code>ParalelGC</code>回收器也是复制算法、多线程、独占式。</p>

<p>区别：</p>

<ul>
<li>非常关注系统的吞量</li>
</ul>

<p>使用方式：</p>

<ul>
<li><code>-XX:+UseParallelGC</code>: 新生代使用<code>ParallelGC</code>回收器，老年代使用串行回收器。</li>
<li><code>-XX:+UseParallelOldGC</code>: 新生代使用<code>ParallelGC</code>回收器，老年代使用<code>ParallelOldGC</code>回收器。</li>
</ul>

<p>控制系统吞量的两个重要参数，<strong>这两个参数相互矛盾的</strong>。</p>

<ul>
<li><code>-XX:MaxGCPauseMilis</code>:设置最大垃圾回收停顿时间。值是一个大于0的整数，ParallelGC在工作时会调整Java堆大小或其他一些参数，尽可能地把停顿时间控制在MaxGCPauseMilis以内。

<ul>
<li>如果把这个值设置的很小，为了达到预期的停顿时间，虚拟机可能会使用一个小较小的堆(小堆比大堆回收快)，这将导致垃圾回收变得很频繁，从而增加了垃圾回收总时间，降低了吞吐量。</li>
<li>默认情况下，VM没有暂停时间目标值。GC的暂停时间主要取决于堆中实时数据的数量与实时数据量。</li>
</ul></li>
<li><code>-XX:GCTimeRatio</code>：设置吞量大小。值是从0到100的整数，如果值设置为n，则系统垃圾收集时间不超过 1/(1+n)，默认值为99(可以增加<code>-XX:+PrintFlagsFinal</code>参数查看该值)</li>
</ul>

<p><code>ParallelGC</code>回收器与<code>ParNew</code>回收器另一个不同是，<code>ParallelGC</code>可以使用 <code>-XX:+UseAdaptiveSizePolicy</code>打开自适应GC调节策略。这种模式下，新生代大小、eden与survivior的比例、晋升老年代的对象年龄等参数会被自动调整，以达到在堆大小、吞吐量和停顿之间的平衡点。手工调优比较困难的场合，可以直接使用这种自适应的方式，仅指定虚拟机的最大堆、目标吞吐量和停顿时间，让虚拟机自己完成调优工作。这个参数的默认是开着的。</p>

<p><code>ParalelGC</code>的回收日志格式：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   [GC[PSYoungGen: xxxK -&gt; xxxK(xxxk)]</code></pre></div>
<ol>
<li><strong>老年代ParallelOldGC回收器</strong></li>
</ol>

<p>也是多线程并发的收集器。</p>

<p>使用方式：<code>-XX+UseParallelOldGC</code></p>

<p>回收日志格式：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   <span style="color:#ce5c00;font-weight:bold">[</span>FULL GC <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen:xxK-&gt;xxK<span style="color:#ce5c00;font-weight:bold">(</span>xxK<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>ParOldGen:xxK-&gt;xxK<span style="color:#ce5c00;font-weight:bold">(</span>xxK<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">]</span></code></pre></div>
<h2 id="三-cms回收器">三、CMS回收器</h2>

<p>CMS回收器主要关注于系统的停顿时间。CMS是 Concurrent Mark Sweep的缩写，意为并发标记清除，使用的是标记清除算法，且多线程并行回收。</p>

<p>CMS 不会对新生代做垃圾回收，默认只针对老年代进行垃圾回收。此外，CMS 还可以开启对永久代的垃圾回收（或元空间），避免由于 PermGen 空间耗尽带来 Full GC。</p>

<ol>
<li><strong>CMS主要工作步骤</strong></li>
</ol>

<p>CMS回收器主要步骤有：</p>

<ul>
<li><p>初始标记</p>

<p>标记根对象</p>

<blockquote>
<p>GC Roots 到底是什么东西呢，哪些对象可以作为 GC Root 呢?</p>

<ul>
<li><p>虚拟机栈（栈帧中的本地变量表）中引用的对象</p></li>

<li><p>本地方法栈中 JNI（即一般说的 Native 方法）引用的对象</p></li>

<li><p>方法区中类静态属性引用的对象</p></li>

<li><p>方法区中常量引用的对象</p></li>
</ul>

<p>参考文档 <a href="https://blog.csdn.net/weixin_38007185/article/details/108093716">GC Roots 是什么？哪些对象可以作为 GC Root？看完秒懂！</a></p>
</blockquote></li>

<li><p>并发标记</p>

<p>标记所有对象</p></li>

<li><p>预清理</p>

<p>是并发的。</p>

<p>为正式清理做准备和检查，还会尝试控制一次停顿时间。</p>

<ul>
<li>刻意等一次新生代GC的发生，然后根据历史性能数据预测下一次新生代GC可能发生的时间，然后在当前旱和预测时间的中间时刻进行得新标记，从最大程序避免新生代GC和重新标记重合，尽量减少一次停顿的时间。</li>
</ul>

<p>可以通过关闭开关 <code>-XX:-CMSPrecleaningEnabled</code> 不进行预清理。</p></li>

<li><p>重新标记</p></li>

<li><p>并发清除</p></li>

<li><p>并发重置</p>

<p><font color=red>并发重置是干嘛？</font></p>

<p>| 独占系统资源 | 可以和用户线程一起执行 |
 | &mdash;&mdash;&mdash;&mdash; | &mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;- |
 | 初始标记     | 预清理                 |
 | 重新标记     | 并发标记               |
 |              | 并发清除               |
 |              | 并发重置               |</p></li>
</ul>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625721430665-20210708131709.png" width = 400 /> </div></p>

<p>整体上说CMS收集不是独占式的，可以在应用程序运行过程中进行垃圾回收。</p>

<ol>
<li><strong>CMS主要的设置参数</strong></li>
</ol>

<p>启用CMS回收器的参数是 <code>-XX:+UseConcMarkSweepGC</code></p>

<p>CMS默认启动的并发线程数是<code>（(ParallelGCThreads+3)/4）</code>。<code>ParallelGCThreads</code>表示GC并行时使用的线程数量 。</p>

<blockquote>
<p>并发指收集器和应用线程交替执行，并行是指应用程序停止，同时由多个线程一起执行GC。因为并行回收器并不是并发的。因为并行回收器执行时，应用程序完全挂起，不存在交替执行的步骤。</p>
</blockquote>

<p>由于CMS回收器不是独占式的回收器，在CMS回收过程中，应用程序仍工作且产生新垃圾，新生成的垃圾在当前CMS回收过程中是无法清除的。同时CMS回收过程中还要确保应用程序有足够的内存可用。<font color=red>因此，CMS回收器不会等堆内在饱和时才进行垃圾回收，而是当堆内存使用率达到某一阈值时便开始进行回收，以确保应用在CMS工作时依然有足够内存支持运行。</font></p>

<p>回收阈值可以使用 <code>-XX:CMSInitiatingOccupancyFraction</code> 来指定当<font color=red>老年代空间</font>使用率到达多少时进行一次CMS垃圾回收。默认是68，即当老年代空间使用率达到68%时会执行一次CMS回收。如果应用程序的内在使用率增长很快，在CMS的执行过程中，已经出现了内存不足的情况，此时，CMS回收就会失效，虚拟机将启动老年代串行收集器进行垃圾回收。如果这样，应用程序STW直到垃圾回收完成，这时应用程序的停顿时间可能会较长。</p>

<p>因此，根据应用程序的特点，可以对<code>-XX:CMSInitiatingOccupancyFraction</code> 进行调优。如果内存增长缓慢，则可以设置一个稍微大的值来降低CMS的触发频率，减少老年代回收次数。相反，如果内存使用率增长很快，则应该降低这个阈值，以避免频繁触发老年代串行收集器。</p>

<p>CMS基于标记清除算法，会造成大量内存碎片，回收后即使堆内存仍有很大剩余空间，也会被迫进行一次垃圾回收来换取可用的连续内存。为此CMS回收器还提供了几个用于内存压缩整理的参数。</p>

<ul>
<li><code>-XX:+UseCMSCompactAtFullCollection</code>开关可以使CMS在垃圾收集完成后进行一次内在碎片整理内存碎片整理不是并发进行的。</li>
<li><code>-XX:CMSFullGCsBeforeCompaction</code> 参数可以用于设定进行多少次CMS回收后进行一次内存压缩。

<br /></li>
</ul>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625813871537-20210709145750.png" width = 400 /> </div></p>

<ol>
<li>CMS日志分析</li>
</ol>

<p>CMS回收的日志可以结合cms工作流程图来看。</p>

<p>这个日志仍是使用<code>StopWorldTest.java</code>的代码跑的，代码及jvm参数在本节下面。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-SHELL" data-lang="SHELL">   <span style="color:#8f5902;font-style:italic">###初始标记</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>CMS Initial Mark<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">1</span> CMS-initial-mark: 524103K<span style="color:#ce5c00;font-weight:bold">(</span>1048064K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 524175K<span style="color:#ce5c00;font-weight:bold">(</span>1048512K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0004341 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   <span style="color:#8f5902;font-style:italic">###并发标记</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-mark-start<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-mark: 0.118/0.150 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.54 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.03, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.15 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   <span style="color:#8f5902;font-style:italic">###预清理</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-preclean-start<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-preclean: 0.015/0.020 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.04 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.02 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   <span style="color:#8f5902;font-style:italic">###重新标记</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>CMS Final Remark<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>YG occupancy: <span style="color:#0000cf;font-weight:bold">112</span> K <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">448</span> K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>Rescan <span style="color:#ce5c00;font-weight:bold">(</span>parallel<span style="color:#ce5c00;font-weight:bold">)</span> , 0.0007684 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>weak refs processing, 0.0000362 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>class unloading, 0.0011978 
   secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>scrub symbol table, 0.0006013 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>scrub string table, 0.0002530 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">1</span> CMS-remark: 530161K<span style="color:#ce5c00;font-weight:bold">(</span>1048064K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 530274K<span style="color:#ce5c00;font-weight:bold">(</span>1048512K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0029381 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: 
   <span style="color:#8f5902;font-style:italic">###并发清理</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-sweep-start<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-sweep: 0.078/0.091 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.18 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.09 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   <span style="color:#8f5902;font-style:italic">###并发重置</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-reset-start<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>CMS-concurrent-reset: 0.005/0.007 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 secs<span style="color:#ce5c00;font-weight:bold">]</span> </code></pre></div>
<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625721430665-20210708131709.png" width = 400 /> </div></p>

<p>CMS回收器在运行时，还可能会输出如下日志</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    (concurrent mode failure): 1047294K-&gt;19809K(1048064K), 0.1619026 secs] 1047666K-&gt;19809K(1048512K), [Metaspace: 8794K-&gt;8794K(1056768K)], 0.1633398 secs] [Times: user=0.20 sys=0.01, real=0.16 secs] </code></pre></div>
<p>这说明CMS回收器并发收集失败。这很可能由于应用程序在运行过程中老年代空间不够导致。如果频繁出现并发模式失败，就应该考虑进行调整预留较大老年代空间或设置一个较小的 <code>-XX:CMSInitiatingOccupancyFraction</code> 参数降低 CMS触发的阈值，使CMS在执行过程中仍有较大的老年代空闲空间供应用程序使用。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">   * 使用的jvm参数如下
</span><span style="color:#8f5902;font-style:italic">   * -Xmx1g -Xms1g -Xmn512k -XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:CMSInitiatingOccupancyFraction=50
</span><span style="color:#8f5902;font-style:italic">   */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">StopWorldTest</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">MyThread</span> <span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">MyThread</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">PrintThread</span> <span style="color:#000">p</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">PrintThread</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">t</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">start</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">p</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">start</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">MyThread</span> <span style="color:#204a87;font-weight:bold">extends</span> <span style="color:#000">Thread</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">HashMap</span> <span style="color:#000">map</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">HashMap</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#5c35cc;font-weight:bold">@Override</span>
           <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">run</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#204a87;font-weight:bold">try</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                   <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#8f5902;font-style:italic">//                    System.out.println((map.size() * 512) / 1024 / 1024);
</span><span style="color:#8f5902;font-style:italic"></span>                       <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">size</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">512</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">880</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                           <span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">clear</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;clean map&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#ce5c00;font-weight:bold">}</span>
                       <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">b1</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">100</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                           <span style="color:#000">b1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">512</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
                           <span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">nanoTime</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">b1</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#ce5c00;font-weight:bold">}</span>
                       <span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">1</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                   <span style="color:#ce5c00;font-weight:bold">}</span>
               <span style="color:#ce5c00;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">catch</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Exception</span> <span style="color:#000">e</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
               <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">PrintThread</span> <span style="color:#204a87;font-weight:bold">extends</span> <span style="color:#000">Thread</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">starttime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#5c35cc;font-weight:bold">@Override</span>
           <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">run</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#204a87;font-weight:bold">try</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                   <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                       <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">starttime</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">1000</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34;.&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">%</span> <span style="color:#000">1000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">100</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                   <span style="color:#ce5c00;font-weight:bold">}</span>
               <span style="color:#ce5c00;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">catch</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Exception</span> <span style="color:#000">e</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
               <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<ol>
<li><strong>Class回收</strong></li>
</ol>

<p>CMS回收器回收Perm区需要打开 <code>-XX:+CMSClassUnloadingEnabled</code>开关，打开后如果条件允许，系统会使用CMS的机制回收Perm区的Class数据。输出日志如下</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   [class unloading, 0.0012854 secs][scrub symbol table, 0.0005998 secs][scrub string table, 0.0002410 secs][1 CMS-remark: 1026980K(1048064K)] 1027325K(1048512K), 0.0028075 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]</code></pre></div>
<h2 id="四-g1回收器">四、G1回收器</h2>

<p>G1回收器（Garbage-First）是JDK1.7正式使用的全新垃圾回收器。既分代（年轻代[eden区、survivor区]、老年代），但堆结构上不要求整个eden区、年轻代、老年代都连续。使用全新的分区算法。</p>

<p>G1收集器使用 <code>-XX:+UseG1GC</code>开启</p>

<p>特点：</p>

<ul>
<li>并行性：多GC线程同时工作</li>
<li>并发性：G1拥有与应用程序交替执行的能力 ，部分工作可以和应用程序同时执行，因此不会在整个回收期间完全阻塞应用程序。</li>
<li>分代GC：同时兼顾新生代和老年代。</li>
<li>空间整理：G1每次回收都会有效地复制对象，减少空间碎片。</li>
<li>可预见性：由于分区的原因，G1可以只选取部分区域进行内存回收，缩小回收的范围，对全局停顿有较好的控制。</li>
</ul>

<h3 id="g1的内存划分和主要收集过程">G1的内存划分和主要收集过程</h3>

<p>G1收集器将堆分区，划分为一个个的区域，每次收集时只收集其中的几个区域，以此来控制垃圾回收产生的一次停顿时间。</p>

<p>G1收集过程可能有4个阶段</p>

<ul>
<li>新生代GC</li>
<li>并发标记周期</li>
<li>混合收集</li>
<li>如果需要可能会进行Full GC</li>
</ul>

<ol>
<li><strong>G1的新生代GC</strong></li>
</ol>

<p>新生代GC收集过程与其他新生代收集器比没什么变化。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   
   sequenceDiagram
   
   eden区 -&gt;&gt; survivor区: eden区部分对象移到survivor区后，eden区被清空
   Note right of survivor区: survivor被收集一部分，至少还存在一个
   eden区 -&gt;&gt; old区: 部分对象晋升到老年代
   survivor区 -&gt;&gt; old区: 部分对象晋升到老年代
   </code></pre></div>
<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625823861743-20210709174420.png" width = 600 /> </div></p>

<ol>
<li><strong>G1并发标记周期</strong></li>
</ol>

<p>G1的并发阶段和CMS有些类似，都是为了降低一次停顿时间而将可以和程序并发的部分单独提取出来执行。</p>

<p>并发标记周期步骤：</p>

<ul>
<li>初始标记</li>
</ul>

<p>标记从根节点直接可达的对象， STW</p>

<blockquote>
<p>GC Roots 到底是什么东西呢，哪些对象可以作为 GC Root 呢？</p>

<ul>
<li><p>虚拟机栈（栈帧中的本地变量表）中引用的对象</p></li>

<li><p>本地方法栈中 JNI（即一般说的 Native 方法）引用的对象</p></li>

<li><p>方法区中类静态属性引用的对象</p></li>

<li><p>方法区中常量引用的对象</p></li>
</ul>

<p>参考文档 <a href="https://blog.csdn.net/weixin_38007185/article/details/108093716">GC Roots 是什么？哪些对象可以作为 GC Root？看完秒懂！</a></p>
</blockquote>

<ul>
<li>根区域扫描</li>
</ul>

<p>由于初始标记必然伴随一次新生代GC，所以在初始化标记后，eden区被清空，存活的对象被移入survivor区。</p>

<p>在根区域扫描这个阶段，将扫描survivor区直接可达老年区的对象并标记。</p>

<blockquote>
<p>tips:根区域扫描不能和新生代GC同时执行（因为根区域扫描依赖survivor区的对象，而新生代GC会修改这个区域），因些，如果恰七在些时需要进行新生代GC，GC就要等待根区域扫描结束后才能进行，如果发生这种情况，这次新生代的GC的时间就会延长。</p>
</blockquote>

<ul>
<li>并发标记</li>
</ul>

<p>与CMS类似，扫描并查找整个堆的存活对象，并做好标记。是并发过程，并可以被一次新生代GC打断。</p>

<ul>
<li>重新标记</li>
</ul>

<p>与CMS一样，重新标记也STW。由于在并发标记过程中应用程序仍在运行，因此标记结果可能需要修正，所以在重新标记过程中对并发标记的结果进行补充。在G1中，这个过长使用SATB（SNAPSHOT-AT-THE-BEGINNING）算法完成，即G1在标记之初为存活对象创建一个快照，这个快照有用于加速重新标记的速度。</p>

<ul>
<li>独占清理</li>
</ul>

<p>这个阶段也STW，它将计算各个区域的存活对象和GC回收比例并进行排序，识别可供混合回收的区域。在这个阶段，还会更新记忆集（Remebered Set）,该阶段给出了需要被混合回收的域名并进行了标记，给混合回收阶段使用。</p>

<blockquote>
<p>记忆集是G1中维护的一个数据结构，简称RS。每个G1区域都有一个RS与之关联。由于G1回收时是按区域回收的，如回收区域A的对象时，很可能并不回收区域B的对象，此时，为了回收区域A的对象 ，要扫描区域B甚至是整个堆来判断区域A中哪些对象不可达，这样代价很大。因此，G1在区域A的RS中，记录了在区域A中被其他区域引用的对象，这样在回收区域A时，只要将RS视为区域A根集中的一部分即可，从而避免做整个堆的扫描。由于系统在运行过程中，对象之间的关系不停变化 ，因此，为了更高效的跟踪这些引用关系，会将这些变化 记录在UPDATE Buffers中。</p>
</blockquote>

<ul>
<li>并发清理阶段</li>
</ul>

<p>识别并清理完全空闲的区域。并发清理，不会引起停顿。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">graph TD
    初始标记 --&gt; 根区域扫描
    根区域扫描 --&gt; 并发标记
    并发标记 --&gt; 重新标记
    重新标记 --&gt; 独占清理
    独占清理 --&gt; 并发清理阶段</code></pre></div>
<p>下图显示并发标记周期前后堆的可能情况，由于并发标记周期包含一次新生代GC（<font color=red>这点是不是真的？上面写的是根区域扫描时会伴随一次新生代GC，而并发标记时可能会被一次新生代GC打断，但并没有说必定会包含一次新生代GC，这里要待确认下</font>），帮新生代会被整理，但由于并发标记周期执行时，应用程序依然在运行，因此，并发标记周期结束后，又会有新的eden空间被使用。并发标记周期执行前后最大的不同是在该阶段后，系统增加了一些标记为G的区域。这些区域被标记，是因为它内部的垃圾比例较高，因此希望在后续的混合GC中进行收集（注意在并发标记周期中并未正式收集这些区域）。这些将要被回收的区域会被G1记录在一个称为Collection Sets(回收集)的集合中。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1626061306055-20210712114144.png" width = 600 /> </div>

<p>并发回收阶段工作流程如下图，初始标记、重新标记、独占清理都是STW的，其他几个阶段都是和应用程序并发执行的。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1626062197846-20210712115637.png" width = 600 /> </div>

<ol>
<li><p><strong>G1日志</strong></p>

<ol>
<li><p>初始标记，伴随一次新生代GC</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"># 初始化标记的日志
[GC pause (G1 Evacuation Pause) (young) (initial-mark), 0.0029507 secs]
 [Parallel Time: 2.5 ms, GC Workers: 10]
    [GC Worker Start (ms): Min: 9905.2, Avg: 9905.2, Max: 9905.3, Diff: 0.2]
    [Ext Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 1.2]
    [Update RS (ms): Min: 1.6, Avg: 1.9, Max: 2.1, Diff: 0.5, Sum: 18.9]
       [Processed Buffers: Min: 2, Avg: 2.4, Max: 3, Diff: 1, Sum: 24]
    [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
    [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
    [Object Copy (ms): Min: 0.1, Avg: 0.3, Max: 0.6, Diff: 0.5, Sum: 2.9]
    [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
       [Termination Attempts: Min: 1, Avg: 1.0, Max: 1, Diff: 0, Sum: 10]
    [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
    [GC Worker Total (ms): Min: 2.2, Avg: 2.3, Max: 2.4, Diff: 0.2, Sum: 23.1]
    [GC Worker End (ms): Min: 9907.5, Avg: 9907.5, Max: 9907.5, Diff: 0.0]
 [Code Root Fixup: 0.0 ms]
 [Code Root Purge: 0.0 ms]
 [Clear CT: 0.1 ms]
 [Other: 0.4 ms]
    [Choose CSet: 0.0 ms]
    [Ref Proc: 0.2 ms]
    [Ref Enq: 0.0 ms]
    [Redirty Cards: 0.1 ms]
    [Humongous Register: 0.0 ms]
    [Humongous Reclaim: 0.0 ms]
    [Free CSet: 0.0 ms]
 # 新生代GC的日志
 [Eden: 1024.0K(1024.0K)-&gt;0.0B(1024.0K) Survivors: 1024.0K-&gt;1024.0K Heap: 461.1M(1024.0M)-&gt;461.8M(1024.0M)]
[Times: user=0.02 sys=0.00, real=0.00 secs] </code></pre></div></li>

<li><p>根区域扫描</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">[GC concurrent-root-region-scan-start]
[GC concurrent-root-region-scan-end, 0.0001889 secs]</code></pre></div></li>

<li><p>并发标记，可以被新生代GC打断。下面</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">## 并发标记开始
[GC concurrent-mark-start]
## 被新生代GC打断
[GC pause (G1 Evacuation Pause) (young), 0.0031241 secs]
 [Parallel Time: 2.5 ms, GC Workers: 10]
    [GC Worker Start (ms): Min: 9974.3, Avg: 9974.3, Max: 9974.4, Diff: 0.2]
    [Ext Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 1.1]
    [Update RS (ms): Min: 1.4, Avg: 1.8, Max: 2.1, Diff: 0.7, Sum: 17.7]
       [Processed Buffers: Min: 1, Avg: 2.5, Max: 4, Diff: 3, Sum: 25]
    [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
    [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
    [Object Copy (ms): Min: 0.0, Avg: 0.3, Max: 0.6, Diff: 0.6, Sum: 2.6]
    [Termination (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 0.9]
       [Termination Attempts: Min: 1, Avg: 10.6, Max: 26, Diff: 25, Sum: 106]
    [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
    [GC Worker Total (ms): Min: 2.2, Avg: 2.2, Max: 2.3, Diff: 0.2, Sum: 22.3]
    [GC Worker End (ms): Min: 9976.6, Avg: 9976.6, Max: 9976.7, Diff: 0.1]
 [Code Root Fixup: 0.0 ms]
 [Code Root Purge: 0.0 ms]
 [Clear CT: 0.1 ms]
 [Other: 0.5 ms]
    [Choose CSet: 0.0 ms]
    [Ref Proc: 0.3 ms]
    [Ref Enq: 0.0 ms]
    [Redirty Cards: 0.1 ms]
    [Humongous Register: 0.0 ms]
    [Humongous Reclaim: 0.0 ms]
    [Free CSet: 0.0 ms]
 [Eden: 1024.0K(1024.0K)-&gt;0.0B(1024.0K) Survivors: 1024.0K-&gt;1024.0K Heap: 462.8M(1024.0M)-&gt;463.2M(1024.0M)]
[Times: user=0.02 sys=0.00, real=0.00 secs] 
。。。省略了几个新生代GC。。。
## 并发标记结束
[GC concurrent-mark-end, 0.1297480 secs]</code></pre></div></li>

<li><p>重新标记，STW</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">[GC remark [Finalize Marking, 0.0003260 secs] [GC ref-proc, 0.0000479 secs] [Unloading, 0.0005812 secs], 0.0030680 secs]
[Times: user=0.02 sys=0.00, real=0.00 secs] </code></pre></div></li>

<li><p>独占清理：重新计算个个区域的存活对象</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">[GC cleanup 467M-&gt;467M(1024M), 0.0016913 secs]
[Times: user=0.01 sys=0.00, real=0.01 secs] </code></pre></div></li>

<li><p>并发清理，根据独占清理计算出的每价目区域的存活对象数理，直接回收已不包含存活对象的区域。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">[GC concurrent-cleanup-start]
[GC concurrent-cleanup-end, 0.0000074 secs]</code></pre></div></li>
</ol></li>

<li><p><strong>混合回收</strong></p></li>
</ol>

<p>混合回收阶段同时回收了新生代和老年代。被清理区域的存活对象会被移动到其他区域，减少空间碎片。</p>

<p>并发标记周期总体回收的比例很低，但G1已经知道哪些区域有比较多的垃圾对象，混合回收阶段就可以专门针对这些区域进行回收。</p>

<blockquote>
<p>G1全称 Garbage First Garbage Collector  垃圾优先的垃圾回收器。就是回收时优先选取垃圾比例高的区域。</p>
</blockquote>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1626067848712-20210712133047.png" width = 600 /> </div></p>

<p>混合回收日志</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   [GC pause (G1 Evacuation Pause) (mixed), 0.0058625 secs]
      [Parallel Time: 5.0 ms, GC Workers: 10]
         [GC Worker Start (ms): Min: 12768.1, Avg: 12768.2, Max: 12768.3, Diff: 0.1]
         [Ext Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.1, Sum: 1.0]
         [Update RS (ms): Min: 1.1, Avg: 1.4, Max: 1.7, Diff: 0.7, Sum: 14.5]
            [Processed Buffers: Min: 2, Avg: 2.4, Max: 4, Diff: 2, Sum: 24]
         [Scan RS (ms): Min: 0.9, Avg: 1.2, Max: 1.6, Diff: 0.7, Sum: 12.1]
         [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Object Copy (ms): Min: 1.7, Avg: 2.0, Max: 2.0, Diff: 0.3, Sum: 19.7]
         [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
            [Termination Attempts: Min: 1, Avg: 1.1, Max: 2, Diff: 1, Sum: 11]
         [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
         [GC Worker Total (ms): Min: 4.7, Avg: 4.7, Max: 4.8, Diff: 0.1, Sum: 47.4]
         [GC Worker End (ms): Min: 12773.0, Avg: 12773.0, Max: 12773.0, Diff: 0.0]
      [Code Root Fixup: 0.0 ms]
      [Code Root Purge: 0.0 ms]
      [Clear CT: 0.2 ms]
      [Other: 0.7 ms]
         [Choose CSet: 0.0 ms]
         [Ref Proc: 0.3 ms]
         [Ref Enq: 0.0 ms]
         [Redirty Cards: 0.2 ms]
         [Humongous Register: 0.0 ms]
         [Humongous Reclaim: 0.0 ms]
         [Free CSet: 0.1 ms]
      [Eden: 1024.0K(1024.0K)-&gt;0.0B(1024.0K) Survivors: 1024.0K-&gt;1024.0K Heap: 540.0M(1024.0M)-&gt;534.7M(1024.0M)]
    [Times: user=0.04 sys=0.01, real=0.01 secs] </code></pre></div>
<p>混合GC会执行多次，直到回收了足够多的内在空间，然后会触发一次新生代GC。新生代GC后又可能会发生一次并发标记周期的处理，最后又引起混合GC的执行，因此整个流程可能如下。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   graph TD
   
   A[年轻代GC] --&gt; B[并发标记周期]
   B --&gt; C[混合GC]
   C --&gt; A
   </code></pre></div>
<ol>
<li><strong>必要时的FULL GC</strong></li>
</ol>

<p>内存不充足情况G1会转入FULL GC。</p>

<p>另外，如果在混合GC时发生空间不足或在新生代GC时survivor区和老年代无法容纳幸存对象，老会导致一次FULL GC。</p>

<ol>
<li><p>G1日志</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">## 发生了一次新生代GC，在初始标记时发生的，耗时0.0029032秒，程序至少暂停了0.0029032秒
[GC pause (G1 Evacuation Pause) (young) (initial-mark), 0.0029032 secs]
## 后续并行时间，表示所有GC线程的总花费时间 2.3ms，垃圾回收工作线程10个
  [Parallel Time: 2.3 ms, GC Workers: 10]
 		## GC线程的执行情况 值都是线程的启动时间 如Min是最小启动时间，在程序运行的XXX秒后启动，Avg平均启动时间 Max最晚启动时间 Diff 最早和最晚相差时间
     [GC Worker Start (ms): Min: 7438.1, Avg: 7438.2, Max: 7438.3, Diff: 0.1]
     ## 根扫描耗时GC线程的耗时
     [Ext Root Scanning (ms): Min: 0.1, Avg: 0.1, Max: 0.2, Diff: 0.1, Sum: 1.1]
     ## 更新记忆集（Remember Set 简称RS）耗时
     [Update RS (ms): Min: 1.3, Avg: 1.7, Max: 2.0, Diff: 0.7, Sum: 16.5]
     	 ## 处理Update Buffers（记录对象间的引用关系的缓存）耗时
        [Processed Buffers: Min: 1, Avg: 2.5, Max: 5, Diff: 4, Sum: 25]
     ## 扫描RS时间
     [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
     ## 这个书上并没有写，后面查找下资料补充一下
     [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
     ## 正式回收时 G1会对被回收区域的对象进行疏散（这个疏散的词用的很好），将存活对象复制到其他区域中，这里是对象复制的耗时
     [Object Copy (ms): Min: 0.0, Avg: 0.3, Max: 0.6, Diff: 0.6, Sum: 3.3]
     ## GC工作线程的终止信息，GC线程花在终止阶段的耗时。在GC线程终止前会看其他GC线程是否有没处理完的数据，如果还有，请求终止的GC线程会帮助它尽快完成，然后再尝试终止。 Termination Attempts展示了每个工作线程尝试终止的次数。
     [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.5]
        [Termination Attempts: Min: 1, Avg: 5.0, Max: 10, Diff: 9, Sum: 50]
     [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
     # GC工作线程耗时
     [GC Worker Total (ms): Min: 2.1, Avg: 2.1, Max: 2.2, Diff: 0.1, Sum: 21.4]
     # GC工作线程的完成时间
     [GC Worker End (ms): Min: 7440.3, Avg: 7440.4, Max: 7440.4, Diff: 0.0]
  [Code Root Fixup: 0.0 ms]
  [Code Root Purge: 0.0 ms]
  # 清空CardTable的时间，RS就是靠 CardTable记录哪些对象是存活的。
  [Clear CT: 0.1 ms]
  # 其他几个任务的耗时
  [Other: 0.5 ms]
  		# 选择 CSet(Collection Sets)的时间，Collection Sets 表示被选取的、将要被回收的区域的集合
     [Choose CSet: 0.0 ms]
     # 处理弱引用、软引用的时间
     [Ref Proc: 0.2 ms]
     # 弱引用、软引用入队时间
     [Ref Enq: 0.0 ms]
     [Redirty Cards: 0.1 ms]
     [Humongous Register: 0.0 ms]
     [Humongous Reclaim: 0.0 ms]
     # 释放被回收的CSet中获取的时间，包括他们的RS
     [Free CSet: 0.0 ms]
  # GC回收的整体情况
  [Eden: 1024.0K(1024.0K)-&gt;0.0B(1024.0K) Survivors: 1024.0K-&gt;1024.0K Heap: 459.1M(1024.0M)-&gt;459.8M(1024.0M)]
[Times: user=0.02 sys=0.00, real=0.00 secs] </code></pre></div></li>

<li><p><strong>G1相关参数</strong></p>

<ol>
<li><p><code>-XX:+UseG1GC</code>: 打开G1收集器开关</p></li>

<li><p><code>-XX:MaxGCPauseMillis</code>: <font color=red>G1设置最重要的参数</font>，指定目标最大停顿时间。如果任何一次停顿超过这个设置值，G1就会尝试调整新生代和老年代的比例，调整堆大小、调整晋升年龄等手段，试图达到预设目标。</p></li>
</ol>

<blockquote>
<p>如果停顿时间缩短，意味着更频繁的新生代GC，老年代为了更短的停顿时间，在混合GC时，一次收集的区域数量也会变少，增加了Full GC的可能性</p>
</blockquote>

<ol>
<li><p><code>-XX:ParallelGCThreads</code> :用于设置并行回收时，GC的工作线程数量</p></li>

<li><p><code>-XX:InitiatingHeapOccupancyPercent</code> :指定当整个堆使用率达到多少时触发并发标记周期的执行，默认值是45.一旦设置，始终都不会被G1收集器修改，如果该值设置的偏大，会导致并发周期迟迟不启动，Full GC的可能性也大大增加。反之，过小的值会使得并发周期非常频繁，大量GC线程抢占CPU，导致应用程序性能下降。</p></li>
</ol></li>
</ol>

<h2 id="五-细节问题">五、细节问题</h2>

<ol>
<li><strong>禁用System.gc()</strong></li>
</ol>

<p><code>System.gc()</code>会显式直接触发Full GC，同时对老年代和新生代进行回收。使用<code>-XX:+DisableExplicitGC</code> 可以禁用显式GC.</p>

<ol>
<li><strong>让System.gc()使用并发回收</strong></li>
</ol>

<p>使用<code>-XX:+ExplicitGCInvokesConcurrent</code> 参数，可以改变<code>System.gc()</code>的默认行为，使用并发方式进行回收。</p>

<ol>
<li><strong>使用并行回收器在Full GC前会额外触发新生代GC</strong></li>
</ol>

<p>目的是减少Full GC的停顿时间，如果不需要可以使用  <code>-XX:-ScavengeBeforeFullGC</code> 参数去除Full GC前的新生代GC。默认是打开不去除的。</p>

<ol>
<li><p><strong>对象何时进入老年代</strong></p>

<ul>
<li><p>对象首次创建时会被放在新生代的eden区，如果没有GC，这些对象不会离开eden区</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">/**
* -Xmx64M -Xms64M -XX:+PrintGCDetails
* @author geym
*/
public class AllocEden {
 public static final int _1K = 1024;
     
 public static void main(String args[]) {
     for (int i = 0; i &lt; 5 * _1K; i++) {
         byte[] b = new byte[_1K];
     }
 }
}</code></pre></div></li>
</ul>

<p>从下面的运行结果可以看到，程序运行结束后，堆内存对象都在eden区中。，其他区域使用量都是0%</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> Heap
  PSYoungGen      total 18944K, used 6898K [0x00000007beb00000, 0x00000007c0000000, 0x00000007c0000000)
  ## 都在这里面
   eden space 16384K, 42% used [0x00000007beb00000,0x00000007bf1bc918,0x00000007bfb00000)
   from space 2560K, 0% used [0x00000007bfd80000,0x00000007bfd80000,0x00000007c0000000)
   to   space 2560K, 0% used [0x00000007bfb00000,0x00000007bfb00000,0x00000007bfd80000)
  ParOldGen       total 44032K, used 0K [0x00000007bc000000, 0x00000007beb00000, 0x00000007beb00000)
   object space 44032K, 0% used [0x00000007bc000000,0x00000007bc000000,0x00000007beb00000)
  Metaspace       used 2700K, capacity 4486K, committed 4864K, reserved 1056768K
   class space    used 289K, capacity 386K, committed 512K, reserved 1048576K</code></pre></div>
<ul>
<li>老年对象进入老年代</li>
</ul>

<p>eden对象年龄达到一定大小，就会自然离开年轻代进入老年代（晋升）。对象年龄由对象经历过的GC次数决定。</p>

<p><code>-XX:MaxTenuringThreshold=15</code>用来决定新生代对象最大年龄，默认为15</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"> <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">  * 最大的晋升年龄
</span><span style="color:#8f5902;font-style:italic">  * MaxTenuringThreshold 默认值15
</span><span style="color:#8f5902;font-style:italic">  * &lt;p&gt;
</span><span style="color:#8f5902;font-style:italic">  * -Xmx1024M -Xms1024M -XX:+PrintGCDetails -XX:MaxTenuringThreshold=15 -XX:+PrintHeapAtGC -XX:-UseAdaptiveSizePolicy
</span><span style="color:#8f5902;font-style:italic">  * 注意，跑的时候要增加 -XX:-UseAdaptiveSizePolicy参数，关掉自调整大小策略，否则会在第8次GC后自动调整from区的大小，然后from区容量到达97%，自动清空然后对象晋升老年代。
</span><span style="color:#8f5902;font-style:italic">  *
</span><span style="color:#8f5902;font-style:italic">  * @author geym
</span><span style="color:#8f5902;font-style:italic">  */</span>
 <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">MaxTenuringThreshold</span> <span style="color:#ce5c00;font-weight:bold">{</span>
     
     <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">_1M</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span><span style="color:#ce5c00;font-weight:bold">;</span>
     <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">_1K</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">1024</span><span style="color:#ce5c00;font-weight:bold">;</span>
     
     <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
         <span style="color:#8f5902;font-style:italic">//创建一个map，往里面放了5M的数据，分5000次放
</span><span style="color:#8f5902;font-style:italic"></span>         <span style="color:#8f5902;font-style:italic">//在map里面新建的这些byte数组有引用就不会被回收
</span><span style="color:#8f5902;font-style:italic"></span>         <span style="color:#000">Map</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">Integer</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">map</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">HashMap</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">Integer</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
         <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">5</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">_1K</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
             <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">_1K</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
             <span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
         <span style="color:#ce5c00;font-weight:bold">}</span>
     
         <span style="color:#8f5902;font-style:italic">//不断申请内存,相当于不断向eden区放数据，eden区满了，就会进行新生代GC
</span><span style="color:#8f5902;font-style:italic"></span>         <span style="color:#8f5902;font-style:italic">//会将eden区的存活数据放到 from区
</span><span style="color:#8f5902;font-style:italic"></span>         <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">k</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">k</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">17</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">k</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
             <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">270</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                 <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">g</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">_1M</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
             <span style="color:#ce5c00;font-weight:bold">}</span>
         <span style="color:#ce5c00;font-weight:bold">}</span>
     <span style="color:#ce5c00;font-weight:bold">}</span>
 <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<p>运行结果如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> ##第一次GC前
 {Heap before GC invocations=1 (full 0):
  PSYoungGen      total 305664K, used 261493K [0x00000007aab00000, 0x00000007c0000000, 0x00000007c0000000)
 ## eden区使用了99%
   eden space 262144K, 99% used [0x00000007aab00000,0x00000007baa5d5b8,0x00000007bab00000)
   from space 43520K, 0% used [0x00000007bd580000,0x00000007bd580000,0x00000007c0000000)
   to   space 43520K, 0% used [0x00000007bab00000,0x00000007bab00000,0x00000007bd580000)
  ParOldGen       total 699392K, used 0K [0x0000000780000000, 0x00000007aab00000, 0x00000007aab00000)
   object space 699392K, 0% used [0x0000000780000000,0x0000000780000000,0x00000007aab00000)
  Metaspace       used 2702K, capacity 4486K, committed 4864K, reserved 1056768K
   class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
 [GC (Allocation Failure) [PSYoungGen: 261493K-&gt;6096K(305664K)] 261493K-&gt;6104K(1005056K), 0.0029389 secs] [Times: user=0.01 sys=0.01, real=0.01 secs] 
 ## 第一次GC后
 Heap after GC invocations=1 (full 0):
  PSYoungGen      total 305664K, used 6096K [0x00000007aab00000, 0x00000007c0000000, 0x00000007c0000000)
 ## eden区被清空
   eden space 262144K, 0% used [0x00000007aab00000,0x00000007aab00000,0x00000007bab00000)
 ## 存活对象移动到from区
   from space 43520K, 14% used [0x00000007bab00000,0x00000007bb0f4020,0x00000007bd580000)
   to   space 43520K, 0% used [0x00000007bd580000,0x00000007bd580000,0x00000007c0000000)
  ParOldGen       total 699392K, used 8K [0x0000000780000000, 0x00000007aab00000, 0x00000007aab00000)
   object space 699392K, 0% used [0x0000000780000000,0x0000000780002000,0x00000007aab00000)
  Metaspace       used 2702K, capacity 4486K, committed 4864K, reserved 1056768K
   class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
 }
 ## 第16次GC前 
 {Heap before GC invocations=16 (full 0):
  PSYoungGen      total 305664K, used 267845K [0x00000007aab00000, 0x00000007c0000000, 0x00000007c0000000)
   eden space 262144K, 99% used [0x00000007aab00000,0x00000007baabd5f0,0x00000007bab00000)
   from space 43520K, 13% used [0x00000007bab00000,0x00000007bb0d4020,0x00000007bd580000)
   to   space 43520K, 0% used [0x00000007bd580000,0x00000007bd580000,0x00000007c0000000)
  ParOldGen       total 699392K, used 8K [0x0000000780000000, 0x00000007aab00000, 0x00000007aab00000)
   object space 699392K, 0% used [0x0000000780000000,0x0000000780002000,0x00000007aab00000)
  Metaspace       used 2702K, capacity 4486K, committed 4864K, reserved 1056768K
   class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
 [GC (Allocation Failure) [PSYoungGen: 267845K-&gt;0K(305664K)] 267853K-&gt;6293K(1005056K), 0.0023287 secs] [Times: user=0.00 sys=0.01, real=0.00 secs] 
 ## 第16次GC后
 Heap after GC invocations=16 (full 0):
  PSYoungGen      total 305664K, used 0K [0x00000007aab00000, 0x00000007c0000000, 0x00000007c0000000)
   eden space 262144K, 0% used [0x00000007aab00000,0x00000007aab00000,0x00000007bab00000)
 ## from区清空
   from space 43520K, 0% used [0x00000007bd580000,0x00000007bd580000,0x00000007c0000000)
   to   space 43520K, 0% used [0x00000007bab00000,0x00000007bab00000,0x00000007bd580000)
 ##这里可以看到老年代的空间由原来的8k使用变为了 6293K使用，说明经历过15次GC的对象晋升到老年代了
  ParOldGen       total 699392K, used 6293K [0x0000000780000000, 0x00000007aab00000, 0x00000007aab00000)
   object space 699392K, 0% used [0x0000000780000000,0x0000000780625558,0x00000007aab00000)
  Metaspace       used 2702K, capacity 4486K, committed 4864K, reserved 1056768K
   class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
 }
     </code></pre></div></li>
</ol>

    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/java" rel="tag" title="java">#java#</a>
    
    <a href="http://www.shutdown.cn/tags/jvm" rel="tag" title="jvm">#jvm#</a>
    
    <a href="http://www.shutdown.cn/tags/%e5%9e%83%e5%9c%be%e5%9b%9e%e6%94%b6" rel="tag" title="垃圾回收">#垃圾回收#</a>
    
    <a href="http://www.shutdown.cn/tags/%e5%86%85%e5%ad%98%e5%88%86%e9%85%8d" rel="tag" title="内存分配">#内存分配#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/pip-configure-multi-repo/" rel="next" title="Pip配置多个源">
        <i class="fa fa-chevron-left"></i> Pip配置多个源
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/one-picture-ok-redis-memory-optimize/" rel="prev" title="一张图搞定redis内存优化及配置">
        一张图搞定redis内存优化及配置 <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"><nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#一-串行回收器">一、串行回收器</a></li>
<li><a href="#二-并行回收器">二、并行回收器</a></li>
<li><a href="#三-cms回收器">三、CMS回收器</a></li>
<li><a href="#四-g1回收器">四、G1回收器</a>
<ul>
<li><a href="#g1的内存划分和主要收集过程">G1的内存划分和主要收集过程</a></li>
</ul></li>
<li><a href="#五-细节问题">五、细节问题</a></li>
</ul></li>
</ul>
</nav></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>